<?php
function axeMailer (
	$from,
	$to,
	$subject,
	$charset = "windows-1251",
	$parts = array("text" => "", "html" => "<html><body></body></html>") ){
	if($from == '' || $to == '' || count($parts) == 0) return false;
	$chars = array(
	    "windows-1251" => "w",
	    "koi8-r" => "k",
	    "iso8859-5" => "i",
	    "x-cp866" => "d",
	    "x-mac-cyrillic" => "m"
	);
	if($charset != "windows-1251"){
		$subject = convert_cyr_string($subject, "w", $chars[$charset]);
		$parts['text'] = convert_cyr_string($parts['text'], "w", $chars[$charset]);
		$parts['html'] = convert_cyr_string($parts['html'], "w", $chars[$charset]);
	}

	$message = "";
	$CRLF = "\n";
	$boundary = md5(uniqid(mt_rand(0, 10000)));
	$altBoundary = "ALT" . $boundary;
	$subject = "=?" . $charset . "?B?" . base64_encode($subject) ."?=";

	$headers = "Return-Path: $from" . $CRLF;
	$headers .= "From: $from" . $CRLF;
	$headers .= "Reply-To: $from" . $CRLF;
	$headers .= "Errors-To: " . $_SERVER['SERVER_ADMIN'] . $CRLF;
	$headers .= "X-Mailer: AXE-Mailer (v0.4)" . $CRLF;
	$headers .= "X-Priority: 3 (Normal)" . $CRLF;
	$headers .= "MIME-Version: 1.0" . $CRLF;
	$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"" . $CRLF;
	$headers .= "This is a multi-part message in MIME format.";

	$message .= "--" . $boundary . $CRLF;
	$message .= "Content-Type: multipart/alternative; boundary=\"$altBoundary\"" . $CRLF . $CRLF;

	if(isset($parts['text']) && $parts['text'] != ''){
		$message .= "--" . $altBoundary . $CRLF;
		$message .= "Content-Type: text/plain; charset=\"$charset\"" . $CRLF;
		$message .= "Content-Transfer-Encoding: 8bit" . $CRLF . $CRLF;
		$message .= wordwrap(trim(str_replace("\r", "", $parts['text']))) . $CRLF . $CRLF;
	}

	if(isset($parts['html']) && $parts['html'] != '') {
		$message .= "--" . $altBoundary . $CRLF;
		$message .= "Content-Type: text/html; charset=\"$charset\"" . $CRLF;
		$message .= "Content-Transfer-Encoding: 8bit" . $CRLF . $CRLF;
		$message .= trim(str_replace("\r", "", $parts['html'])) . $CRLF. $CRLF;
	}
	$message .= "--" . $altBoundary . "--" . $CRLF . $CRLF;

	if(isset($parts['attachments']) && count($parts['attachments']) > 0){
		foreach ($parts['attachments'] as $fname) {
			if(file_exists($fname)){
				$message .= "--" . $boundary . $CRLF;
				$message .= "Content-Type: application/octet-stream; name=\"" . basename($fname) . "\"" . $CRLF;
				$message .= "Content-Transfer-Encoding: base64" . $CRLF;
				$message .= "Content-Disposition: attachment; filename=\"" . basename($fname) . "\"" . $CRLF . $CRLF;
				$fp = fopen($fname, "rb");
				$content = fread($fp, filesize($fname));
				fclose($fp);
				$message .= chunk_split(base64_encode($content));
			}
		}
	}
	$message .= "--" . $boundary . "--" . $CRLF;

	return mail ($to, $subject, $message, $headers);
}
?>